﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EZMemoLib;
using Microsoft.Windows.Controls;
using System.ComponentModel;

namespace Remember.WPF.Views
{
    /// <summary>
    /// Interaction logic for LoginControl.xaml
    /// </summary>
    public partial class LoginControl : UserControl
    {
        public event EventHandler OnStart;
        public event EventHandler OnAddLogin;

        public LoginControl()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                Build();
            } 
        }

        public void Build()
        {
            if (loginItem1 != null)
            {
                loginItem1.OnStart += new EventHandler(loginItem1_OnStart);
                loginItem1.OnRemove += new EventHandler(loginItem1_OnRemove);
            }

            var list = Context.GetLoginList();
            if (list != null && list.Count > 0)
            {
                this.pnlLoginList.Children.Clear();

                foreach (var login in list)
                {
                    var loginItem = new LoginItem(login);
                    loginItem.OnStart += new EventHandler(loginItem1_OnStart);
                    loginItem.OnRemove += new EventHandler(loginItem1_OnRemove);
                    this.pnlLoginList.Children.Add(loginItem);
                }
            }
        }

        void loginItem1_OnRemove(object sender, EventArgs e)
        {
            if (sender is MemoLogin)
            {
                var currentLogin = sender as MemoLogin;
                if (currentLogin != null)
                {
                    Context.RemoveLogin(currentLogin.LoginID);
                    Build();
                }
            }            
        }

        void loginItem1_OnStart(object sender, EventArgs e)
        {
            if (sender is MemoLogin)
            {
                var currentLogin = sender as MemoLogin;
                if (currentLogin != null)
                {
                    Context.Login = currentLogin;
                    //Context.LoginID = currentLogin.LoginID;

                    if (OnStart != null)
                    {
                        OnStart(sender, e);
                    }
                }
            }
        }

        private void btnAddauser_Click(object sender, RoutedEventArgs e)
        {
            if (OnAddLogin != null)
            {

                OnAddLogin( this, e);
            }

            //var loginAdd = new LoginAdd();
            //loginAdd.OnClose += new EventHandler((a, b) => { childWindow.Close(); Build(); });

            //childWindow.Content = loginAdd;

            //childWindow.WindowStartupLocation = Microsoft.Windows.Controls.WindowStartupLocation.Center;

            //if (loginAdd.Height > 0)
            //{
            //    childWindow.Height = loginAdd.Height + 40;
            //}

            //if (loginAdd.Width > 0)
            //{
            //    childWindow.Width = loginAdd.Width;
            //}
            //childWindow.Show();
        }

        void loginAdd_OnClose(object sender, EventArgs e)
        {
            
        }

    }

}
